package com.sxf.rental.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxf.rental.entity.Role;
import com.sxf.rental.entity.RolePermission;
import com.sxf.rental.entity.User;
import com.sxf.rental.entity.UserRole;
import com.sxf.rental.mapper.RoleMapper;
import com.sxf.rental.mapper.RolePermissionMapper;
import com.sxf.rental.mapper.UserMapper;
import com.sxf.rental.mapper.UserRoleMapper;
import com.sxf.rental.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sxf
 * @since 2025-03-08
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private RolePermissionMapper rolePermissionMapper;
    @Override
    public Page<Role> selectList(Page<Role> page, Role role) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(role.getRoleName()), "role_name", role.getRoleName());
        queryWrapper.orderByAsc("create_time");
        Integer userId = role.getCreaterId();
        User user = userMapper.selectById(userId);
        if (user != null && user.getIsAdmin() != 1) { //1是管理员
            queryWrapper.eq("creater_id", userId);
        }
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public boolean hasUser(Integer id) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", id);
        userRoleMapper.selectList(queryWrapper);
        return userRoleMapper.selectCount(queryWrapper)>0;
    }

    @Override
    public boolean delete(String ids) {
        List<Integer> list = Arrays.stream(ids.split(",")).map(Integer::parseInt).toList();
        if (!list.isEmpty()){
            for (Integer i : list) {
                if (!hasUser(i)){
                    rolePermissionMapper.delete(new QueryWrapper<RolePermission>().eq("role_id", i));
                    baseMapper.deleteById(i);
                }
            }
        }
        return true;
    }

    @Override
    public boolean assignPermission(Integer roleId, List<Integer> permissionIds) {
        //删除原有的关联权限
        QueryWrapper<RolePermission> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        rolePermissionMapper.delete(queryWrapper);
        //插入新数据
        if (!permissionIds.isEmpty()){
            for (Integer permissionId : permissionIds) {
                RolePermission rolePermission=new RolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(permissionId);
                rolePermissionMapper.insert(rolePermission);
            }
        }
        return true;
    }

    @Override
    public List<Integer> selectRoleIdByUserId(Integer userId) {
        return baseMapper.selectRoleIdByUserId(userId);
    }
}
